package generic;

import org.junit.Test;

import java.util.*;

/**
 * @author lwmzzz
 * @version 1.0
 * @date 2021/4/2 0:41
 * @description 泛型
 */
public class GenericTest {
    @Test
    public void test() {
        List a = new ArrayList();
        List<String> b = new ArrayList<>();

        a.add("1");
        a.add(1);
        a.add('1');
        a.add(1L);
        a.add(1D);
        a.add(1F);
        for (Object o : a) {
            System.out.println(o.getClass());
        }
    }

    @Test
    public void test1() {
        class Father<T1, T2> {

        }

        class Son1 extends Father {
            //equals class Son1 extends Father<Object,Object>
        }
        class Son2<T1, T2> extends Father<T1, T2> {
            //equals class Son1 extends Father<T1, T2>
        }

        class Son3<Integer, T2> extends Father<Integer, T2> {
        }

        class Son4<Integer, String> extends Father<Integer, String> {
        }
    }

    @Test
    public void test2() {
        class Person<T> {
            private T data;

            public T getData() {
                return data;
            }

            public Person() {
            }

            public void setData(T data) {
                this.data = data;
            }

            public Person(T data) {
                this.data = data;
            }

            public <W> List<W> copyToList(W[] array) {
                List<W> list = new ArrayList<>();
                for (W e : array) {
                    list.add(e);
                }
                return list;
            }
        }
        Person<String> a = new Person<>();
//        List<Integer> integers = a.copyToList(new Integer[]{1, 23, 4, 5});
    }

    @Test
    public void test3() {
        List list = new ArrayList();
        list.add("12");
        list.add("123");
        list.add("1234");
        printCollection(list);
        printCollection1(list);
    }

    @Test
    public void test4() {
        Integer[] array = {1, 2, 3, 4};
    }

    @Test
    public void test5() {
        List<?> list = new ArrayList<>();
        list.add(null);
//        list.add("1");
    }

    @Test
    public void test6() {
        List<? extends Student> list = new ArrayList<>();
//        list.add(new Object())
        List<? super Student> list1 = new ArrayList<>();
    }

    public static void printCollection(Collection c) {
        Iterator iterator = c.iterator();
        for (int i = 0; i < c.size(); i++) {
            System.out.println(iterator.next());
        }
    }

    public static void printCollection1(Collection<Object> c) {
        for (Object o : c) {
            System.out.println(o);
        }
    }

    public static void shoo(Collection<?> collection) {

    }

    public <E> List<E> show(E[] list) {
        return Collections.emptyList();
    }
}